var app = getApp()
var ImageURL = app.globalData1.ImageURL;
var hostURL = app.globalData1.hostURL;
Page({
  data:{

    statusType: ["待付款"],

    //statusType: ["待付款", "待服务", "待评价", "已完成"],

    currentType:0,
    tabClass: ["", "", "", "", ""],
    orderList:[],
    userId:'',
    orderId:'',
    payOrder:[]
  },
  statusTap:function(e){
     var curType =  e.currentTarget.dataset.index;
     this.data.currentType = curType
     this.setData({
       currentType:curType
     });
     //this.onLoad();
  },
//   orderDetail : function (e) {
//     var orderId = e.currentTarget.dataset.id;
//     wx.navigateTo({
//       url: "/pages/order-details/index?id=" + orderId
//     })
//   },
  
  cancelOrderTap: function (e) {
     var that = this;
     var orderId = e.currentTarget.dataset.id;
     console.log(orderId)
     wx.showModal({
        title: '确定要取消该订单吗？',
        content: '',
        success: function (res) {
           if (res.confirm) {
              wx.showLoading();
              wx.request({
                 url: hostURL + 'sharedplatform-web/orderMaster/removeOrder.action',
                 data: {
                    orderId: orderId
                 },
                 method: 'get',
                 header: {
                    'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
                 },
                 success: (res) => {
                    var errno = res.data.errno
                    if (errno == 0) {
                       console.log("取消订单成功")
                       wx.showToast ({               
                          title: '取消订单成功',
                          content: '',
                       })
                       wx.navigateTo({
                          url: "/pages/order-list/index?userId=" + wx.getStorageSync('userInfo').id });
                       
                    } else {
                       wx.showToast({
                          title: '取消订单失败',
                          content: '',
                       })
                    }
                 },
                 
              })
           }
        }
     })

  },
//   onShow: function () {
//      this.cancelOrderTap();
//   },
  toPayTap: function (e) {
     var that = this;
     var orderId = e.currentTarget.dataset.id;
     that.setData({
        orderId: orderId
     })
     //console.log(orderId)
     wx.showModal({
        title: '确定付款吗？',
        content: '',
        success: function (res) {
         //   if (res.confirm) {
              wx.showLoading();
              wx.request({
                 url: hostURL + 'sharedplatform-web/orderMaster/payOrder.action',
                 data: {
                    orderId: that.data.orderId
                 },
                 method: 'get',
                 header: {
                    'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
                 },
                 success: (res) => {
                   
                    var errno = res.data.errno
                    if (errno == 0) {
                       console.log("付款成功")
                       wx.showToast({
                          title: '付款成功',
                          content: '',
                       })
                       
                       wx.navigateTo({
                          url: "/pages/waitservice/waitservice?userId=" + wx.getStorageSync('userInfo').id
                       });

                    } 
                    else if (errno == 1){
                       wx.showToast({
                          title: '订单为空,付款失败',
                          content: '',
                       })
                    }
                    else if(errno == 2) {
                       wx.showToast({
                          title: '订单信息错误,付款失败',
                          content: '',
                       })
                    }
                  },

              })
          // }
        }
     })
  },
//   
  onLoad:function(option){
     var that = this;
     that.setData({
        userId: option.userId,
     });
     // 获取订单列表
     wx.request({
        url: hostURL + 'sharedplatform-web/orderMaster/findOrder.action',
        method: 'post',
        data: {
           userId: that.data.userId,
        },
        header: {
           'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        success: (res) => {
         //   wx.hideLoading();
           var errno = res.data.errno
           if (errno == 0) {
              console.log("订单查询成功")
              //if (res.data.code == 0) {
              that.setData({
                 orderList: res.data.orderListVO,
              }); 
              //}
            //  console.log(that.data.orderList)
            //  var orderId = that.data.orderList.orderId
            //  console.log(orderId)
           } else if (errno == 1) {
              console.log("改服务不在服务区")
              wx.showToast({
                 title: '您还没有订单',
                 content: '',
              })

           }else if(errno==3){
              console.log("您还没有订单哦亲！")
              wx.showToast({
                 title: '您还没有订单',
                 content: '',
              })

           }
        }
     })
  },
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
  },
  onHide:function(){
    // 生命周期函数--监听页面隐藏
 
  },
  onUnload:function(){
    // 生命周期函数--监听页面卸载
 
  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作
   
  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数
  
  }
})
